/*
 * Copyright 2010 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.anim.updater.geom
{
	import flash.display.DisplayObject;
	import flash.geom.ColorTransform;
	import sweezy.anim.core.IPropertyAnimation;
	import sweezy.anim.core.IUpdater;
	import sweezy.anim.updater.UpdaterProperty;

	public class ColorTransformUpdater implements IUpdater
	{

		public function ColorTransformUpdater()
		{
		}

		public function initialize(element:Object, property:UpdaterProperty, prefix:String, option:Object, target:IPropertyAnimation):void
		{
			var ct:ColorTransform = (element is DisplayObject) ? (element as DisplayObject).transform.colorTransform : (element as ColorTransform);
			if (element is DisplayObject && ct === null)
			{
				ct = new ColorTransform();
				(element as DisplayObject).transform.colorTransform = ct;
			}

			while (property !== null)
			{
				if (isNaN(property.end))
				{
					property.end = 0;
				}
				if (isNaN(property.start))
				{
					property.name === "alphaMultiplier" ? property.start = ct.alphaMultiplier :
						property.name === "redMultiplier" ? property.start = ct.redMultiplier :
						property.name === "greenMultiplier" ? property.start = ct.greenMultiplier :
						property.name === "blueMultiplier" ? property.start = ct.blueMultiplier :
						property.name === "alphaOffset" ? property.start = ct.alphaOffset :
						property.name === "redOffset" ? property.start = ct.redOffset :
						property.name === "greenOffset" ? property.start = ct.greenOffset :
						property.name === "blueOffset" ? property.start = ct.blueOffset :
						property.start = 0;
				}
				if (property.relative)
				{
					property.end += property.start;
				}

				property = property.next;
			}
		}

		public function update(element:Object, property:UpdaterProperty, prefix:String, fraction:Number, option:Object, target:IPropertyAnimation):void
		{
			var ct:ColorTransform = (element is DisplayObject) ? (element as DisplayObject).transform.colorTransform : (element as ColorTransform);
			var value:Number;
			if (element is DisplayObject && ct === null)
			{
				ct = new ColorTransform();
			}

			while (property !== null)
			{
				value = property.start + (property.end - property.start) * fraction;
				value = property.round ? ((value * 2 | 0) - (value | 0)) : value;

				property.name === "alphaMultiplier" ? ct.alphaMultiplier = value :
					property.name === "redMultiplier" ? ct.redMultiplier = value :
					property.name === "greenMultiplier" ? ct.greenMultiplier = value :
					property.name === "blueMultiplier" ? ct.blueMultiplier = value :
					property.name === "alphaOffset" ? ct.alphaOffset = value :
					property.name === "redOffset" ? ct.redOffset = value :
					property.name === "greenOffset" ? ct.greenOffset = value :
					property.name === "blueOffset" ? ct.blueOffset = value :
					null;
				property = property.next;
			}

			if (element is DisplayObject)
			{
				(element as DisplayObject).transform.colorTransform = ct;
			}
		}
	}
}
